use std::char;

pub fn convert_base(num_str: &str, to_base: u32) -> String {
    let mut parts = num_str.splitn(2, '(');
    let value_str = parts.next().unwrap();
    let from_base_str = parts.next().map(|s| s.trim_end_matches(')')).unwrap_or("10");
    let from_base = from_base_str.parse::<u32>().unwrap_or_default();

    let decimal = match from_base {
        2 => i64::from_str_radix(value_str, 2).unwrap_or_default(),
        8 => i64::from_str_radix(value_str, 8).unwrap_or_default(),
        16 => i64::from_str_radix(value_str, 16).unwrap_or_default(),
        _ => i64::from_str_radix(value_str, from_base).unwrap_or_default(),
    };

    if decimal == 0 {
        return "0".to_string();
    }

    let mut result = String::new();
    let mut n = decimal;
    while n > 0 {
        let remainder = n % to_base as i64;
        result.push(char::from_digit(remainder as u32, to_base).unwrap_or('0'));
        n /= to_base as i64;
    }
    result.chars().rev().collect()
}